<template>
  <div class="rules-wrapper">
    <div class="top">
      <div class="title-wrapper">
        <div class="title">代言规则设置</div>
      </div>
    </div>
    <div class="content">
      <el-form ref="form" :model="form" label-width="80px" size="mini">
        <el-form-item label="代言规则:" prop="rule">
          <Editor :content="form.rule" @catchData="handleCatchData" />
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" @click="confirm">确定</el-button>
  </div>
</template>
<script>
import Editor from '@/common/editor'
import { setRules, getRules } from '@/api/represent'
const ERR_OK = 200
export default {
  components: {
    Editor
  },
  data() {
    return {
      form: {
        rule: ''
      }
    }
  },
  created() {
    this._getRules()
  },
  methods: {
    handleCatchData(content) {
      console.log(content)
      this.form.rule = content
    },
    confirm() {
      this._setRules(this.form.rule)
    },
    _setRules(represent_commodity_rule) {
      setRules(represent_commodity_rule)
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this.$message({
              type: 'success',
              message: res.message,
              showClose: true
            })
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _getRules() {
      getRules()
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this.form.rule = res.data.represent_commodity_rule
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.rules-wrapper{
  padding: 0 20px;
  box-sizing: border-box;
  .top {
    width: 100%;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    .title-wrapper {
      display: flex;
      align-content: center;
      .title {
        height: 44px;
        line-height: 44px;
        color: rgba(68, 68, 68, 1);
        font-size: 18px;
        font-family: Roboto;
        font-weight: bold;
        cursor: pointer;
      }
    }
  }
  #wangeditor{
    width: 815px;
    height: 505px;
  }
}
</style>
